package com.example.lock.entity;

import com.example.util.TranslationUtils;
import lombok.Data;
import org.springframework.transaction.TransactionStatus;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: Redis 实体类
 * @Author: mingtian
 * @CreateDate: 2020/12/11 16:29
 * @Version: 1.0
 */
@Data
public class RedisLockInfo {
    /**
     * 锁的状态 state 为true 则表示获取锁成功
     */
    private boolean state;

    /**
     * 锁的id
     */
    private String lockId;

    /**
     * 锁的持有线程
     */
    private Thread lockThread;

    /**
     * 锁的过期时间
     */
    private Long expire;

    /**
     * 续命次数
     */
    private AtomicInteger lifeCount;

    /**
     * 记录事务信息
     */
    private TranslationUtils translationUtils;

    /**
     * 事务信息
     */
    private TransactionStatus transactionStatus;

    // 锁的可重入次数

    public Integer getLifeCount() {
        return lifeCount.incrementAndGet();
    }

    public RedisLockInfo() {
        state = true;
        lifeCount = new AtomicInteger(0);
    }

    public RedisLockInfo(Thread lockThread) {
        this.lockThread = lockThread;
        lifeCount = new AtomicInteger(0);
        state = true;
    }

    public RedisLockInfo(Thread lockThread, Long expire) {
        this.lockThread = lockThread;
        this.expire = expire;
        lifeCount = new AtomicInteger(0);
        state = true;
    }

    public RedisLockInfo(String lockId, Thread lockThread, Long expire) {
        state = true;
        this.lockId = lockId;
        this.lockThread = lockThread;
        this.expire = expire;
        lifeCount = new AtomicInteger(0);
    }

    public RedisLockInfo(String lockId, Thread lockThread, Long expire, TranslationUtils translationUtils, TransactionStatus transactionStatus) {
        this.state = true;
        this.lockId = lockId;
        this.lockThread = lockThread;
        this.expire = expire;
        lifeCount = new AtomicInteger(0);
        this.translationUtils = translationUtils;
        this.transactionStatus = transactionStatus;
    }
}